﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Controls;
using System.Windows.Input;
using EDMDesigner.Designer.Controls;
using EDMDesigner.Designer.Controls.CSDLType;
using EDMDesigner.Designer.Controls.CSDLType.Resources;
using EDMDesigner.Designer.Controls.Menu;
using EDMDesigner.Designer.Controls.Util;
using EDMDesigner.UIBusiness;
using EDMDesigner.UIBusiness.CSDL.Type;

namespace EDMDesigner.Designer
{
    partial class EDMDesignerWindow
    {
        private const string CONTEXTMENU_RESOURCE_NAME = "CSDLTypeContextMenu";
        //private const string MENUITEM_RENAME = "renameMenuItem";
        private const string MENUITEM_DELETE = "deleteMenuItem";
        private const string MENUITEM_PROPERTIES = "propertiesMenuItem";
        private const string MENUITEM_MAPPING = "mappingMenuItem";
        private const string MENUITEM_SHOW_TABS = "showTabsMenuItem";

        private Controls.Menu.ContextMenu CSDLTypeContextMenu
        {
            get
            {
                return FindResource(CONTEXTMENU_RESOURCE_NAME) as Controls.Menu.ContextMenu;
            }
        }

        //private static RoutedCommand _renameCommand;
        //public static RoutedCommand RenameCommand
        //{
        //    get
        //    {
        //        if (_renameCommand == null)
        //            _renameCommand = new RoutedCommand();
        //        return _renameCommand;
        //    }
        //}
        //public static string RenameText
        //{
        //    get
        //    {
        //        return MenuResources.Rename;
        //    }
        //}

        private static RoutedCommand _deleteCommand;
        public static RoutedCommand DeleteCommand
        {
            get
            {
                if (_deleteCommand == null)
                    _deleteCommand = new RoutedCommand();
                return _deleteCommand;
            }
        }
        public static string DeleteText
        {
            get
            {
                return MenuResources.Delete;
            }
        }

        private static RoutedCommand _propertiesCommand;
        public static RoutedCommand PropertiesCommand
        {
            get
            {
                if (_propertiesCommand == null)
                    _propertiesCommand = new RoutedCommand();
                return _propertiesCommand;
            }
        }
        public static string PropertiesText
        {
            get
            {
                return MenuResources.Properties;
            }
        }

        private static RoutedCommand _mappingCommand;
        public static RoutedCommand MappingCommand
        {
            get
            {
                if (_mappingCommand == null)
                    _mappingCommand = new RoutedCommand();
                return _mappingCommand;
            }
        }
        public static string MappingText
        {
            get
            {
                return MenuResources.Mapping;
            }
        }

        private static RoutedCommand _showTabsCommand;
        public static RoutedCommand ShowTabsCommand
        {
            get
            {
                if (_showTabsCommand == null)
                    _showTabsCommand = new RoutedCommand();
                return _showTabsCommand;
            }
        }
        public static string ShowTabsText
        {
            get
            {
                return MenuResources.ShowTabs;
            }
        }

        private static RoutedCommand _showTabsItemCommand;
        public static RoutedCommand ShowTabsItemCommand
        {
            get
            {
                if (_showTabsItemCommand == null)
                    _showTabsItemCommand = new RoutedCommand();
                return _showTabsItemCommand;
            }
        }

        private void InitCommandBinding()
        {
            var commandBindings = new[] 
            { 
                //new CommandBinding(RenameCommand, (sender, e) => ((EditableTextBlock)e.OriginalSource).Edit()),
                new CommandBinding(DeleteCommand, (sender, e) => 
                    {
                        var uiType = getSelectedUIType(sender);
                        uiType.View.DeleteType(uiType);
                    }),
                new CommandBinding(PropertiesCommand, delegate { ShowPropertiesTab(); }),
                new CommandBinding(MappingCommand, delegate { ShowMappingTab(); }),
                new CommandBinding(ShowTabsCommand,
                        delegate { },
                        (sender, e) =>
                        {
                            var showOtherTabsMenuItem = CSDLTypeContextMenu.GetMenuItem(MENUITEM_SHOW_TABS);
                            var uiType = getSelectedUIType(sender);
                            var designerViews = (VisualTreeHelperUtil.GetControlAscendant<EDMDesignerWindow>(sender)).DesignerViewsCloneCurrent.Where(dv => dv.TypeDesignersLocations.Any(tdl => tdl.UIType.BusinessInstance == uiType.BusinessInstance));
                            e.CanExecute = designerViews.Any();
                            if (e.CanExecute)
                                TypeBaseDesigner.ShowDesignerCanvasPreviews(EDMView, uiType, showOtherTabsMenuItem, designerViews);
                            else
                                showOtherTabsMenuItem.Items.Clear();
                        }
                    ),
                    new CommandBinding(ShowTabsItemCommand,
                        (sender, e) => 
                            {
                                var value = ((DesignerView)e.Parameter).CloneOrThis;
                                if (CurrentDesignerView == value)
                                {
                                    CurrentDesignerSurface.UnselectAllTypes();
                                    CurrentDesignerSurface.TypesVisibles.First(t => t.UIType == getSelectedUIType(sender)).IsSelected = true;
                                }
                                else
                                    CurrentDesignerView = value;
                            }
                    )
            };
            entityTypesListView.CommandBindings.AddRange(commandBindings);
            hierarchyFilterTreeView.CommandBindings.AddRange(commandBindings);
            complexTypesListView.CommandBindings.AddRange(commandBindings);
        }

        private IEnumerable<DesignerView> DesignerViewsCloneCurrent
        {
            get
            {
                foreach (var designerView in EDMView.DesignerViews)
                {
                    if (designerView == (DesignerView)designerTab.SelectedItem)
                    {
                        var value = DesignerCanvas.DesignerInCache.FirstOrDefault(dv => dv.DesignerViewCloned == designerView);
                        if (value != null)
                            yield return value;
                        else
                        {
                            value = DesignerViewRW.Reader.Read(EDMView, entityType => new EntityTypeDesigner(entityType), complexType => new ComplexTypeDesigner(complexType), DesignerViewWriter.Writer.Write(designerView));
                            value.DesignerViewCloned = designerView;
                            yield return value;
                        }
                    }
                    else
                        yield return designerView;
                }
            }
        }

        private IUIType getSelectedUIType(object container)
        {
            var listView = container as ListView;
            if (listView != null)
                return (IUIType)listView.SelectedItem;
            else
            {
                var treeView = container as TreeView;
                if (treeView != null)
                    return (IUIType)treeView.SelectedItem;
                else
                    throw new NotImplementedException();
            }
        }
    }
}
